<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          
          <a-col :md="8" :sm="24">
            <a-form-item label="入库单号">
              <a-input v-model="queryParam.orderNumber" placeholder="请输入入库单号"/>
            </a-form-item>
          </a-col>
          
          <a-col :md="8" :sm="24">
            <a-form-item label="生产单号">
              <a-input v-model="queryParam.sourceOrderNumber" placeholder="请输入生产单号"/>
            </a-form-item>
          </a-col>
          
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons"
                  :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <!--<a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>-->
            </span>
          </a-col>
          
          <a-col :md="24" :sm="24">
            <a-form-item label="归属公司：">
              <!--              <a-radio-group default-value="" button-style="solid" v-model="queryParam.orgIdCompany">-->
              <!--                <a-radio-button value="">-->
              <!--                  全部-->
              <!--                </a-radio-button>-->
              <!--                <a-radio-button :value="id" v-for="{id, name} in orgs">-->
              <!--                  {{ name }}-->
              <!--                </a-radio-button>-->
              <!--              </a-radio-group>-->
              <FCOrgRadioGroup v-model="queryParam.targetOrgId"
                               :filter-types="[$businessEnum.ORG_TYPE_ENUM.COMPANY.id]"/>
            </a-form-item>
          </a-col>
        
        </a-row>
      </a-form>
    </div>
    
    <div class="table-operator">
    </div>
    
    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.code"
      :rowSelection="null"
    >

      <span slot="action" slot-scope="text, record">
        <a @click="$refs.inStoreModal.show(record)">入库</a>
				<a-divider type="vertical"/>
        <a @click="$refs.remarkForm.init('edit', record)">备注</a>
        
      </span>
    </s-table>
    
    <in-store-modal ref="inStoreModal" @ok="handleOk"/>
    <remark-form ref="remarkForm" @ok="handleOk"/>
  </a-card>
</template>

<script>
import {STable} from '@/components'
import {
  page,
  changeStatus,
  del,
  doScheduleProduction,
} from '@/api/modular/main/emsorder/emsOrderManage'


import inStoreModal from './inStoreModal'
import remarkForm from './remarkForm'

import FCOrgRadioGroup from '@/components/FcOrgRadioGroup/FCOrgRadioGroup'

export default {
  components: {
    STable,
    inStoreModal,
    FCOrgRadioGroup,
    remarkForm
  },
  data() {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},  // 查询参数
      columns: [       // 表头
        {
          title: '序号', width: '80px',
          dataIndex: '',
          customRender: (text, record, index) => `${index + 1}`
        },
        {
          title: '入库单号',
          width: '230px',
          dataIndex: 'orderNumber',
          ellipsis: true
        },
        {
          title: '生产单号',
          width: '230px',
          dataIndex: 'sourceOrderNumber',
          ellipsis: true
        },
        {
          title: '生产单归属公司',
          dataIndex: 'targetOrgName'
        },
        {
					//生产单排单时间
          title: '完成生产时间',
          dataIndex: 'inProgressTime'
        },
        // {
        //   title: '入库单创建时间',
        //   dataIndex: ''
        // },
        {
          title: '创建人',
          dataIndex: 'createUserName'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.queryParam.orderType = this.$businessEnum.ORDER_TYPE_ENUM.productionProductInOrder.id
        this.queryParam.executeStatus = this.$businessEnum.EXECUTE_STATUS_ENUM.IN_PROGRESS.id
        this.queryParam.isOverseasStore = 'N'
        return page(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      scheduleProductionVisible: false,
      selectedRowKeys: [],
      selectedRows: [],
      defaultExpandedKeys: [],
      replaceFields: {
        key: 'id',
      },
      urlParam: {},
      columnsType: '',
      suppliers: [],
      orgs: [],
      viewState: '',
      materialType: '',
    }
  },
  methods: {
    del(record) {
      del(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败：' + res.message)
        }
      }).catch((err) => {
        this.$message.error('删除错误：' + err.message)
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    onOrderStatusChange(checked, row) {
      row.loading = true
      let {orderId} = row
      let status = checked ? 0 : 1
      let parameter = {orderId, status}
      changeStatus(parameter).then(res => {
        this.handleOk()
      })
    },
    // 排单生产
    handleScheduleProductionShow() {
      if (this.validateScheduleProduction()) {
        this.scheduleProductionVisible = true
      }
    },
    validateScheduleProduction() {
      let selectedRows = this.selectedRows
      if (selectedRows.length == 0) {
        this.$message.error('请先选择要排单生产的订单')
        return false
      }
      let unavailableOrders = selectedRows.filter(value => value.status != 0)
      if (unavailableOrders.length > 0) {
        this.$message.error('订单状态必须为"可用"才可以进行排单')
        return false
      }
      return true
    },
    handleOkByScheduleProduction() {
      // 调接口,排单生产
      if (this.validateScheduleProduction()) {
        let orderIds = this.selectedRows.map(value => value.orderId).join(',')
        doScheduleProduction({orderIds}).then(res => {
          this.$message.success(res.message)
        })
      }
    },
  }
}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
